package name.matthewgreet.example6.model; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.SessionFactory; public abstract class AbstractEntity<D extends Serializable> { private SessionFactory sessionFactory; protected AbstractEntity() { super(); } void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void flush() { if (sessionFactory == null) { throw new IllegalStateException("Entity must be added to table before flushing"); } try (Session session = sessionFactory.getCurrentSession()) { session.beginTransaction(); session.update(this); session.getTransaction().commit(); } } public abstract D getDTO(); }